using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ToDoListWeb.Models;
using ToDoListWeb.ViewModels;

namespace ToDoListWeb.Service
{
    public class InventoryService
    {
        private ToDoListWebContext _ctx = new ToDoListWebContext();
        TypeService _typeService = new TypeService();

        /// <summary>
        /// 创建清单；
        /// 根据所给数据新建一个Inventory对象；
        /// 将该对象添加到user下的Inventory列表和_ctx下的Inventory列表中；
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="inventoryName"></param>
        public void CreateInventory(int typeId, string inventoryName)                    //创建清单
        {
            TypeService Type = new TypeService();
            var type = Type.GetType(typeId);
            Inventory inventory = new Inventory()
            {
                UserId = type.UserId,
                InventoryName = inventoryName,
                TypeId = type.TypeId
            };
            _ctx.Inventory.Add(inventory);
            _ctx.SaveChanges();
        }
        /// <summary>
        /// 删除清单；
        /// 若在该user中的inventory列表中无该inventoryId匹配项，抛出异常；
        /// 若匹配到该Inventory对象，则删除该清单以及该清单下所有事件；
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="inventoryId"></param>
        public void DeleteInventory(int userId, int inventoryId)                                           //删除清单
        {
            var inventory = _ctx.Inventory.Include("Matter").SingleOrDefault(m => m.UserId == userId && m.InventoryId == inventoryId);
            var matters = inventory.Matter.ToList();
            _ctx.Inventory.Remove(inventory);
            _ctx.Matter.RemoveRange(matters);
            _ctx.SaveChanges();
        }
        
        /// <summary>
        /// 通过inventoryId获取该清单对象；
        /// 若没有该inventoryId匹配项，抛出异常；
        /// 若匹配到该Inventory对象，返回该对象；
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="inventoryId"></param>
        /// <returns></returns>
        public Inventory GetInventoryById(int inventoryId)                                       //获得一个清单
        {
            var inventory = _ctx.Inventory.Include("Matter").FirstOrDefault(p => p.InventoryId == inventoryId);
            //var inventory = _ctx.Inventory.SingleOrDefault(inven => inven.InventoryId == inventoryId);
            return inventory;
        }
       
        public List<Inventory> GetInventorysByUserId(int userId)
        {
            return _ctx.Users.Include("Inventory").Include("Matter").SingleOrDefault(u => u.UserId == userId).Inventory.ToList();
        }

        public void CompileInventoryModel(int typeId, int inventoryId, string inventoryName)
        {
            var inventory = _ctx.Inventory.SingleOrDefault(i => i.InventoryId == inventoryId);
            inventory.InventoryName = inventoryName;
            inventory.TypeId = typeId;
            _ctx.SaveChanges();

        }

        public AddInventoryModel InventoryToAddInventoryModel(UserModel user, int typeId)
        {
            AddInventoryModel inventoryModel = new AddInventoryModel()
            {
                User = user,
                Type = _typeService.GetType(typeId)
            };
            return inventoryModel;
        }
    }
}
